package com.study.hadoop.weather;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * 排序
 *  年月正序，温度倒序
 * @author stephen
 * @create 2019-01-10 21:40
 * @since 1.0.0
 */
public class WeatherSortComparator extends WritableComparator {

    public WeatherSortComparator() {
        // true：实例化Weather
        super(Weather.class, true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        Weather w1 = (Weather) a;
        Weather w2 = (Weather) b;

        int y = Integer.compare(w1.getYear(), w2.getYear());
        if (y == 0) {
            int m = Integer.compare(w1.getMonth(), w2.getMonth());
            if (m == 0) {
                return -Integer.compare(w1.getTemp(), w2.getTemp());
            }
            return m;
        }
        return y;
    }
}
